<?php
/////////////////////////////////////////////////////////////////////////////
// 
//
// workflow_system 
//
// workflow_copyright
// 
/////////////////////////////////////////////////////////////////////////////

/**
 * FILE_NAME : xmlwrite.class.php   FILE_PATH : \classes\libraries\xmlwrite.class.php
 * XML操作类
 *
 * @copyright workflow_system 
 * @author workflow Develop Team 
 * @package 
 * @subpackage 
 * @version Wed Aug 08 14:47:24 CST 2007
 */

	/*
	+------------------------------------------------------------------------------------------------------
	|   功能说明： 插入/修改/新建XML文件.
	|   调用部分 ：
	|   ========================================
	|   $XML=new XML();
	|	$Attribute['date']=date('Y-m-d');
	|	$Attribute['time']=date('H:i:s');
	|	$XML->CreateNode('DocumentROOT',$Attribute);
	|	$Att['author']='Lazy';
	|	$XML->AppendNode('Parent',$Att);
	|	$XML->AppendNode('Child',$Att,'this is text test node.',false);
	|	$XML->AppendNode('Child',$Att,'this is text test node.',false);
	|	$XML->AppendNode('Child',$Att,'this is text test node.',false);
	|	$XML->AppendNode('Child',$Att,'this is text test node.',false);
	|	$Att['Email']='o0lazy0o@gmail.com';
	|	$XML->AppendNode('ParentII',$Att);
	|	$XML->AppendNode('Child',null,'this is text test node.',false);
	|	$XML->AppendNode('Child',null,'this is text test node.',false);
	|	$XML->AppendNode('Child',null,'this is text test node.',false);
	|	$XML->AppendNode('Child',null,'this is text test node.',false);
	|	$XML->Save('example.xml');
	|	$XML->Display();
	|    
	|    
	|    显示结果：
	|
	|	<?xml version="1.0" encoding="utf-8"?>
	|	<DocumentROOT date="2007-08-03" time="10:49:54">
	|	<Parent author="Lazy">
	|	<Child author="Lazy">this is text test node.</Child>
	|	<Child author="Lazy">this is text test node.</Child>
	|	<Child author="Lazy">this is text test node.</Child>
	|	<Child author="Lazy">this is text test node.</Child>
	|	</Parent>
	|	<ParentII author="Lazy" Email="o0lazy0o@gmail.com">
	|	<Child>this is text test node.</Child>
	|	<Child>this is text test node.</Child>
	|	<Child>this is text test node.</Child>
	|	<Child>this is text test node.</Child>
	|	</ParentII>
	|	</DocumentROOT>
	|
	+------------------------------------------------------------------------------------------------------
	*/
Class XML{
    var $Content="";
    var $RootNode="";
    var $ParentNode="";
    var $CRLF="\r\n";
    var $End="";
    
    /**
     * 构造函数:生成默认XML头
     * 返回值:true
     */
    Function XML($Version="1.0",$Encoding="utf-8"){
        $this->Content.="<?xml version=\"{$Version}\" encoding=\"{$Encoding}\"?>{$this->CRLF}";
    }

    /**
     * CreateNode函数:创建根节点
     * $NodeName为节点名称
     * $Attribute为节点的属性
     * 返回值为处理后的内容
     */
    Function CreateNode($NodeName="root",$Attribute=""){
        $NodeName=$this->Filter($NodeName);
        $this->RootNode=$NodeName;
        $Attribute=$this->ParseAttribute($Attribute);
        return $this->Content.="<{$NodeName}{$Attribute}>{$this->CRLF}";
    }

    /**
     * AppendNode函数:添加节点
     * 如果没有数据的话为父节点,有为带数据的节点
     * $NodeName为节点名称
     * $Attribute为节点的属性
     * 返回值为处理后的内容
     */
    Function AppendNode($NodeName,$Attribute,$Data="",$CDate=true){
        $NodeName=$this->Filter($NodeName);
        if($Data==""){
            if(!empty($this->ParentNode)){
                $this->Content.="</{$this->ParentNode}>{$this->CRLF}";
            }
            $this->ParentNode=$NodeName;
            $Attribute=$this->ParseAttribute($Attribute);
            return $this->Content.="<{$NodeName}{$Attribute}>{$this->CRLF}";
        }else{
            $Attribute=$this->ParseAttribute($Attribute);
            return $this->Content.=$CDate?"<{$NodeName}{$Attribute}>{$this->CRLF}<![CDATA[{$Data}]]>{$this->CRLF}</{$NodeName}>{$this->CRLF}":"<{$NodeName}{$Attribute}>{$Data}</{$NodeName}>{$this->CRLF}";
        }
    }
    
    /**
     * End函数:添加XML结束节点字符串
     * 返回完整的XML文档内容
     */
    Function End(){
        if($this->End){
            return $this->Content;
        }else{
            $this->End=true;
            return $this->Content=$this->ParentNode==""?$this->Content."</{$this->RootNode}>":$this->Content."</{$this->ParentNode}>{$this->CRLF}</{$this->RootNode}>";
        }
    }

    /**
     * Display函数:输出XML文档
     * 无返回值
     */
    Function Display(){
        ob_start();
        header("Content-type: text/xml");
        echo $this->End();
        ob_end_flush();
    }

    /**
     * Save函数:保存XML文档
     * 保存路径默认为程序当前目录,可在$Filename指定路径如:SITEROOT."/archives/save.xml"(注意权限问题)
     * 返回值:true或者false
     */
    Function Save($Filename){
        if(!$Handle=fopen($Filename,'wb+')){
        	$lang_obj = new FrameWork();
        	$lang_obj->setFrameWork();
        	$lang_obj->getlang("sys_common");
            $this->Error($lang_obj->_lang['langSysCNotCreateFileWrite']);//不能创建文件或写入文件请检查权限
            unset($lang_obj);
        }
        flock($Handle,LOCK_EX);
        fwrite($Handle,$this->End());
        return fclose($Handle);
    }

    /**
     * Error函数:处理错误信息
     */
    Function Error($ErrorStr='',$ErrorNo='',$Stop=true){
        exit($ErrorStr);
    }

    /**
     * ParseAttribute函数:解析节点属性数组 
     * $Argv为要解析的内容数组,注意此数组只能为一维数组
     * 返回值为解析过后的内容
     */
    Function ParseAttribute($Argv){
        $Attribute='';
        if(is_array($Argv)){
            foreach($Argv as $Key=>$Value){
                if(is_array($Value)){
                	$lang_obj = new FrameWork();
        			$lang_obj->setFrameWork();
        			$lang_obj->getlang("sys_common");
                    $this->Error($lang_obj->_lang['langSysCAttributeErr']);//属性错误:存放属性的数组只能是一维数组
                    unset($lang_obj);
                }
                $Value=$this->Filter($Value);
                $Attribute.=" $Key=\"$Value\"";
            }
        }
        return $Attribute;
    }
    
    /**
     * Filter函数:过滤字符
     * 返回处理完的内容
     */
    Function Filter($Argv){
        $Argv=trim($Argv);
        $Search=array("<",">","\"");
        $Replace=array("","","'");
        return str_replace($Search,$Replace,$Argv);
    }
}


?>